define([
        "jquery",
        "model/SlideObj",
        "model/ContentObj",
        "view/SlideEditorView",
        "util/ControlBox",
        "util/ControlPoint",
        'lib/snap.svg'
], function($, SlideObj, ContentObj, SlideEditorView, ControlBox, ControlPoint, Snap) {
	
	function SlideEditorController(slideList) {
		
		this.slideEditorView = new SlideEditorView();
		this.mainController = null;
		this.slideList = slideList;
		
		/* ****140318 **** */
		this.slideSVG = null;
		this.selectedShape = null;
		this.drawMode = false;
		this.drawType = null;
		this.controlBoxObj = null;
		this.controlPoint  = [];
		this.isFullScreen = false;
	};
	
	SlideEditorController.prototype = {
			
		callRender : function() {//start			
				this.createControlBox();
				this.slideEditorView.doRender(this.slideList);
		},
		
		setMainController : function(mainController) {
			this.mainController = mainController;
			this.slideEditorView.setController(mainController);
		},
		
		setSlideList : function(slideList) {
			this.slideList = slideList;
		},
		
		drawWorkSpaceSlide: function () {
			var slideIndex = this.mainController.currentSlideNum-1;
			this.slideEditorView.drawWorkSlide(this.slideList[slideIndex].getContentList());
		},
		
		showMode: function(event) {
			this.isFullScreen = true;
			var that = this;
			var svgObj = document.getElementById("workspace");
			
			if (svgObj.webkitRequestFullscreen) {
				svgObj.webkitRequestFullscreen();
				
				if(this.isFullScreen == true) {
					this.slideEditorView.drawShowBtnBox();
					$(svgObj).bind("click",$.proxy(this.screenClick,this));
					$(document).bind(
						"keyup",
						function(event) {
							if (event.keyCode == "27") {
								$('#showBtnBox').remove();
								that.isFullScreen = false;
								$(svgObj).unbind("click");
							}
						}
					);
				}		  
			}
			else {
		        console.log("Fullscreen API is not supported");
		    } 
		},
		
		screenClick: function() {
			if(this.mainController.currentSlideNum > this.slideList.length+1) {
				console.log("currentSlideNum 범위 벗어남");
			} else {
				this.mainController.getThumbnailController().thumbnailView.removeBoundary(this.mainController.currentSlideNum);
				this.mainController.currentSlideNum++;
				this.mainController.getThumbnailController().thumbnailView.displayBoundary(this.mainController.currentSlideNum);
				this.slideEditorView.drawWorkSlide(this.slideList[this.mainController.currentSlideNum-1].getContentList());
			}
		},
		
		showModeBtnClick: function (e) {
			e.stopPropagation();
			var btnType = e.target.id;
			
			if(btnType=='next') {
				this.mainController.getThumbnailController().thumbnailView.removeBoundary(this.mainController.currentSlideNum);
				this.mainController.currentSlideNum++;
				this.mainController.getThumbnailController().thumbnailView.displayBoundary(this.mainController.currentSlideNum);
				
			} else if(btnType=='previous') {
				this.mainController.getThumbnailController().thumbnailView.removeBoundary(this.mainController.currentSlideNum);
				this.mainController.currentSlideNum--;
				this.mainController.getThumbnailController().thumbnailView.displayBoundary(this.mainController.currentSlideNum);
			}
			
			this.slideEditorView.drawWorkSlide(this.slideList[this.mainController.currentSlideNum-1].getContentList());
		},
		
		deleteWorkSpaceSlide: function(deleteNum) {
			$('#slide_svg').children().remove();
		},
		
		getDrawMode : function() {
			return this.drawMode;
		},
		
		setDrawMode : function(flag) {
			this.drawMode = flag;
		},
		
		setDrawType : function(event) {
			this.drawMode = true;
			this.drawType = event.target.id;
		},
		
		getDrawType : function() {
			return this.drawType;
		},
		
		//선택된 도형정보를 받아와 너비와, 높이를 구하고, 좌표값을 구한 다음
		//컨트롤 박스에 해당 정보들을 setter 메소드를 통해 저장
		setSelectedContent : function(event) {
			this.selectedShape = event.target;			
			var w = this.selectedShape.getBoundingClientRect().width;
			var h = this.selectedShape.getBoundingClientRect().height;
			var x = this.selectedShape.getBoundingClientRect().left-220;
			var y = this.selectedShape.getBoundingClientRect().top-102;
			this.controlBoxObj.setSize(w, h);
			this.controlBoxObj.setCoordinate(x, y);
			
			//도형이 아닌 슬라이드 배경이 선택된 경우
			if(this.selectedShape.id.indexOf('workSlide') > -1) {
				this.selectedShape = null;
			}

			var _this = this;
			
			
			
			this.slideEditorView.drawControlBox(this.selectedShape, this.controlBoxObj, this.controlPoint);
		},
		
		
		
		
		
		
		
		
		getSelectedShape : function() {
			return this.selectedShape;
		},
		
		getControlBoxObj : function() {
			return this.controlBoxObj;
		},
		
		createControlBox : function() {
			this.controlBoxObj = new ControlBox();
			var position = [
			                'topLeft', 'topMid', 'topRight',
			                'midLeft', 'midRight',
			                'bottomLeft', 'bottomMid', 'bottomRight'
			];
			
			for(var i = 0; i < 8; i++) {
				var pointTemp = new ControlPoint();
				pointTemp.setControlBox(this.controlBoxObj);
				pointTemp.setPosition(position[i]);
				this.controlPoint.push(pointTemp);
			}
		},
		
		createContent : function(content) {
			var contentTemp = new ContentObj();
			var contentId = content.attr().id;
			var contentType = content.attr().type;
			var contentAttr = content.attr().node.attributes;
			var slideIndex = this.mainController.currentSlideNum-1;
			var currentSlide = this.slideList[slideIndex];
			
			//
			contentTemp.setId(contentId);		//id
			contentTemp.setContentType(contentType);	//type()
			contentTemp.setAttributeObj(contentAttr);	//attribute()
			
			currentSlide.addContent(content);
		},
		
		removeContent : function(event) {
			//selectedContent가 있는 경우(null이 아닌 경우)
			var contentTemp = event.target;
			
			if(this.selectedContent) {
				for(var i = 0; i < this.slideList.length; i++) {
					if(this.slideList[i] === contentTemp) {
						console.log(slideList[i]);
					}
				}
			}
		},
		
		updateContentFill : function(event) {
			if(this.selectedShape !== null) {
				this.slideEditorView.drawFillColor(event.target.value, this.selectedShape);
			}
			else {
				alert("도형을 먼저 선택해 주세요");
			}
		},
		
		updateContentOpacity : function(event) {
			if(this.selectedShape !== null) {
				this.slideEditorView.drawFillOpacity(event.target.value, this.selectedShape);
			}
			else {
				alert("도형을 먼저 선택해 주세요");
			}
		},
		
		updateContentStroke : function(event) {
			if(this.selectedShape !== null) {
				this.slideEditorView.drawStrokeColor(event.target.value, this.selectedShape);
			}
			else {
				alert("도형을 먼저 선택해 주세요");
			}
		},
		
		updateStrokeStyle : function(event) {
			if(this.selectedShape !== null) {
				this.slideEditorView.drawStrokeStyle(event.target.value, this.selectedShape);
			}
			else {
				alert("도형을 먼저 선택해 주세요");
			}
		},
		
		updateStrokeWidth : function(event) {
			if(this.selectedShape !== null) {
				this.slideEditorView.drawStrokeWidth(event.target.value, this.selectedShape);
			}
			else {
				alert("도형을 먼저 선택해 주세요");
			}
		}
		
		
	};
	
	return SlideEditorController;

});